#include "com_lora.h"
#include "kservice.h"

static com_lora_cb_t com_lora_cb;

void com_lora_register_callback(com_lora_cb_t cb)
{
  com_lora_cb = cb;
}

void com_lora_isr(void)
{
	unsigned char temp_Rx=0;

	if(USART_GetITStatus(COM_LORA_UART_PORT, USART_IT_RXNE) == SET)
	{
		temp_Rx = USART_ReceiveData(COM_LORA_UART_PORT);     
		USART_ClearFlag(COM_LORA_UART_PORT,USART_FLAG_RXNE);
		USART_ClearITPendingBit(COM_LORA_UART_PORT,USART_IT_RXNE);
//		raw_printf("%c", temp_Rx);
		if(com_lora_cb != 0)
		{
				com_lora_cb(temp_Rx);
		}
	}
}

void com_lora_init(void)
{
	USART_InitTypeDef USART_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	RCC_APB2PeriphClockCmd(COM_LORA_UART_RCC_GPIO , ENABLE); 
  RCC_APB1PeriphClockCmd(COM_LORA_UART_RCC,ENABLE); 

	GPIO_InitStructure.GPIO_Pin = COM_LORA_UART_GPIO_TX;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(COM_LORA_UART_PORT_GPIO, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = COM_LORA_UART_GPIO_RX;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(COM_LORA_UART_PORT_GPIO, &GPIO_InitStructure);

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	USART_InitStructure.USART_BaudRate = COM_LORA_UART_BAUD;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
	USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
	USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
	USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
	USART_Init(COM_LORA_UART_PORT, &USART_InitStructure);
	USART_ClockInit(COM_LORA_UART_PORT, &USART_ClockInitStructure);

	com_lora_cb = 0;
	USART_ITConfig(COM_LORA_UART_PORT, USART_IT_RXNE, ENABLE);
	USART_Cmd(COM_LORA_UART_PORT, ENABLE);
}

void com_lora_put_byte(unsigned char ch)
{
  unsigned short err = 0xffff;
	/* Wait until end of transmit */
	while((USART_GetFlagStatus(COM_LORA_UART_PORT, USART_FLAG_TXE) == RESET)&&(err--))
	{
	}
	/* Write one byte in the USART1 Transmit Data Register */
	USART_SendData(COM_LORA_UART_PORT, ch);
}
